package com.boot2.core.sms;

import com.boot2.core.HlAssert;
import com.boot2.core.conf.SmsTaobaoConfig;
import com.boot2.core.conf.SmsTencentConfig;
import com.github.qcloudsms.SmsSingleSender;
import com.github.qcloudsms.SmsSingleSenderResult;
import com.github.qcloudsms.httpclient.HTTPException;
import com.boot2.core.utils.StringUtils;
import com.taobao.api.ApiException;
import com.taobao.api.DefaultTaobaoClient;
import com.taobao.api.TaobaoClient;
import com.taobao.api.request.AlibabaAliqinFcSmsNumSendRequest;
import com.taobao.api.response.AlibabaAliqinFcSmsNumSendResponse;
import lombok.extern.apachecommons.CommonsLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author : zouzhiyong
 * @description : 短信发送
 * @date : 2019/5/31 14:36
 */
@CommonsLog
@Service
public class SmsSendService {

    @Autowired
    SmsTaobaoConfig smsTaobaoConfig;

    @Autowired
    SmsTencentConfig smsTencentConfig;

    /**
     * @param mobile          手机号
     * @param smsSignName     短信签名
     * @param smsTemplateCode 短信模板Code
     * @param smsParam        模板短信参数内容（JSON字符串，如："{\"code\":\"" + no.toString() + "\",\"product\":\"同业云\"}"）
     * @return 返回发送结果Map,{code:'0',msg:'ok'},code=0 时成功，其他为失败
     * @description 发送阿里模板短信
     * @author zouzhiyong
     * @date 2019/5/31 14:41
     */
    public Map<String, Object> sendTaobao(String mobile, String smsSignName, String smsParam, String smsTemplateCode) {
        Map<String, Object> result = new HashMap<>();
        result.put("code", "-1");
        result.put("msg", "发送失败");
        try {
            HlAssert.isTrue(smsTaobaoConfig != null && StringUtils.isNotEmpty(smsTaobaoConfig.getAppkey()), "sms.taobao configuration can not be null");
            HlAssert.isTrue(StringUtils.isNotEmpty(mobile), "mobile con not be null");
            HlAssert.isTrue(StringUtils.isNotEmpty(smsSignName), "smsSignName con not be null");
            HlAssert.isTrue(StringUtils.isNotEmpty(smsParam), "smsParam con not be null");
            HlAssert.isTrue(StringUtils.isNotEmpty(smsTemplateCode), "smsTemplateCode con not be null");

            TaobaoClient client = new DefaultTaobaoClient(smsTaobaoConfig.getUrl(), smsTaobaoConfig.getAppkey(), smsTaobaoConfig.getSecret());
            AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();
            req.setSmsType("normal");
            req.setSmsFreeSignName(smsSignName);
            req.setSmsParamString(smsParam);
            req.setRecNum(mobile);
            req.setSmsTemplateCode(smsTemplateCode);
            AlibabaAliqinFcSmsNumSendResponse rsp = client.execute(req);
            // 返回阿里发送结果
            result.put("code", rsp.getResult().getErrCode());
            result.put("msg", rsp.getResult().getMsg());
        } catch (ApiException e) {
            log.error("阿里短信发送异常：", e);
            e.printStackTrace();
            result.put("msg", "阿里短信发送异常，请联系管理员");
        }
        return result;
    }

    /**
     * @description 发送腾讯模板短信
     * @author zouzhiyong
     * @param mobile 手机号
     * @param nationCode  国家号
     * @param templateId 模板ID
     * @param paramList 模板参数
     * @param sign  签名
     * @date 2019/5/31 17:09
     */
    public Map<String, Object> sendQq(String mobile, String nationCode, Integer templateId, List<String> paramList, String sign) {
        Map<String, Object> result = new HashMap<>();
        result.put("code", "-1");
        result.put("msg", "发送失败");
        try {
            HlAssert.isTrue(smsTencentConfig != null && StringUtils.isNotEmpty(smsTencentConfig.getAppkey()), "sms.qq configuration can not be null");
            HlAssert.isTrue(StringUtils.isNotEmpty(mobile), "mobile con not be null");
            HlAssert.isTrue(sign != null, "sign con not be null");
            HlAssert.isTrue(templateId != null, "templateId con not be null");
            SmsSingleSender ssender = new SmsSingleSender(smsTencentConfig.getAppid(), smsTencentConfig.getAppkey());
            SmsSingleSenderResult rsp = ssender.sendWithParam(nationCode, mobile, templateId, (ArrayList<String>) paramList, sign,
                    "", "");
            // 返回发送结果
            result.put("code", rsp.result);
            result.put("msg", rsp.errMsg);
        } catch (IOException e) {
            log.error("腾讯短信发送网络 IO 错误：", e);
            e.printStackTrace();
            result.put("msg", "腾讯短信发送异常，请联系管理员");
        } catch (HTTPException e) {
            log.error("腾讯短信发送HTTP 响应码错误：", e);
            e.printStackTrace();
            result.put("msg", "腾讯短信发送异常，请联系管理员");
        }
        return result;
    }
    /**
     * @description 发送腾讯模板短信(国内)
     * @author zouzhiyong
     * @param mobile 手机号
     * @param templateId 模板ID
     * @param paramList 模板参数
     * @param sign  签名
     * @date 2019/5/31 17:09
     */
    public Map<String, Object> sendQqChina(String mobile, Integer templateId, List<String> paramList, String sign) {
        return sendQq(mobile,"86",templateId,paramList,sign);
    }
}
